﻿
Imports System.Data.SqlClient

Public Class FormSubject

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)



    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        RefreshData()

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        DataGridView1.Rows.Clear()
        Dim cn As New SqlConnection
        cn.ConnectionString = "server.;uid=sa;pwd=123;database=SchoolDb;"
        cn.Open()

        Dim cmd As New SqlCommand
        cmd.Connection = cn
     '   cmd.CommandText("WINCTRL-U2MOVFA\SQLEXPRESS")

    End Sub

    Public Sub RefreshData()

        DataGridView1.Rows.Clear()

        Dim cn As SqlConnection
        cn = New SqlConnection
        cn.ConnectionString = "server=.\SQLEXPRESS;uid=sa;pwd=123;database=SchoolDb;"
        cn.Open()

        ' MsgBox(cn.State.ToString())
        Dim cmd As SqlCommand
        cmd = New SqlCommand()
        cmd.Connection = cn
        cmd.CommandText = "SELECT * FROM TBL_SUBJECT WHERE SUBJECT_CODE+SUBJECT_NAME LIKE '%" & TextBox1.Text & "%'"
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader()
        While dr.Read() = True
            Dim code = dr(0)
            Dim name = dr(1)
            Dim type = dr(2)
            Dim price = dr(3)
            DataGridView1.Rows.Add(code, name, type, price)
        End While 

        dr.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim row As DataGridViewRow = DataGridView1.SelectedRows(0)
        Dim code = row.Cells(0).Value

        Dim cn As SqlConnection
        cn = New SqlConnection()
        cn.ConnectionString = "server=.;uid=sa;pwd=123;database=SchoolDb;"
        cn.Open()

        Dim cmd As SqlCommand
        cmd = New SqlCommand()
        cmd.Connection = cn
        cmd.CommandText = "DELETE FROM TBL_SUBJECT WHERE SUBJECT_CODE='" & code & "'"
        cmd.ExecuteNonQuery()

        RefreshData()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        FormSubjectEditor.Show()


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim row As DataGridViewRow
        row = DataGridView1.SelectedRows(0)
        FormSubjectEditor.txtCODE.Enabled = False
        FormSubjectEditor.txtCODE.Text = row.Cells(0).Value
        FormSubjectEditor.txtNAME.Text = row.Cells(1).Value
        FormSubjectEditor.cboTYPE.Text = row.Cells(2).Value
        FormSubjectEditor.txtPRICE.Text = row.Cells(3).Value

        FormSubjectEditor.Show()

    End Sub
End Class
